package com.red.service.impl;

import com.red.pojo.entity.User;
import com.red.pojo.model.RegisterBody;
import com.red.service.IRegisterService;
import com.red.service.IUserService;
import com.red.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements IRegisterService {

    @Autowired
    private IUserService userService;

    @Override
    public Result register(RegisterBody registerBody) {

        String account = registerBody.getAccount();
        String username = registerBody.getUserName();
        String password = registerBody.getPassword();
        String email = registerBody.getEmail();

        String msg = "";

        if (account == null || account.isEmpty()) {
            msg = "账号为空";
        } else if (username == null || username.isEmpty()) {
            msg = "用户名为空";
        } else if (password == null || password.isEmpty()) {
            msg = "密码为空";
        } else {
            if (account.length() < 6 || account.length() > 12) {
                msg = "账号长度不符要求";
            } else if (username.length() < 2 || username.length() > 12) {
                msg = "用户名长度不符要求";
            } else if (password.length() < 6 || password.length() > 24) {
                msg = "密码长度不符要求";
            } else {
                if (userService.selectUserByAccount(account) == null) {
                    User user = new User();
                    user.setAccount(account);
                    user.setUserName(username);
                    user.setEmail(email);
                    user.setPassword(password);

                    if (userService.insertUser(user) > 0) {
                        return Result.success("注册成功");
                    } else {
                        msg = "注册意外失败";
                    }
                } else {
                    msg = "用户已存在";
                }
            }
        }

        return Result.error(msg);
    }
}